package whackEm.elements;

import java.util.ArrayList;
import java.util.Random;

import whackEm.V2.WhackEmPanel;
import whackEm.persistent.GameState;


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.Log;

public class Mole { 
	 	 
	 private static final String TAG = Mole.class.getSimpleName();
	 public static final int RABBIT = 1;
	 public static final int SQRL = 0;
	 protected ArrayList<Bitmap> rabbitComingOutBitmaps = new ArrayList<Bitmap>();
	 protected ArrayList<Bitmap> rabbitDazedBitmaps = new ArrayList<Bitmap>();
	 private ArrayList<Bitmap> sqrlComingOutBitmaps = new ArrayList<Bitmap>();
	 private ArrayList<Bitmap> sqrlDazedBitmaps = new ArrayList<Bitmap>();	 
	 
	 protected Bitmap toDrawBitmap ;
	 private boolean touched = false;
	 protected int hole = 0;
	 protected float draw_x = 0;
	 protected float draw_y = 0; 
	
 	 protected int rabbitComingOutframeNr;		// number of frames in coming out animation
 	 protected int rabbitDazedFrameNr;		// number of frames in dazed animation 	 
 	 protected int currentFrame;	// the current frame 	 
	 protected int repeatingFrames=0;
 	 protected long frameTicker;	// the time of the last frame update
 	 protected int framePeriod;	// milliseconds between each frame (1000/fps) 	 
 	 protected boolean comingOut = false ; //whether the mole is to be drawn or not 
 	 protected boolean dazed = false ; //whether  the mole is to be drawn dazed or not
	 protected int Animal;		 
	 private float scale = 0;
	 protected Random r = new Random();
	 private WhackEmPanel panel;
	 private float width=0,height=0;
	 protected GameState gs;
	 
	 public void setAnimations(){
		 this.rabbitComingOutBitmaps = WhackEmPanel.rabbitComingAnimation;		 		 
		 this.rabbitDazedBitmaps = WhackEmPanel.rabbitDazedAnimation;
		 this.sqrlComingOutBitmaps = WhackEmPanel.sqrlComingAnimation;		 		 
		 this.sqrlDazedBitmaps = WhackEmPanel.sqrlDazedAnimation;		 
	 }
	 

	 public Mole(int hole,int fps, WhackEmPanel panel,GameState gs) {		 
		 //setAnimations();
		 this.panel = panel;
		 this.gs = gs;
		 setAnimal();	 
		 rabbitComingOutframeNr = rabbitComingOutBitmaps.size();		 
		 rabbitDazedFrameNr = rabbitDazedBitmaps.size();
		 this.hole = hole;
		 setHole(hole,this.Animal);
		 currentFrame = 0 ;		 
		 framePeriod = 1000/fps;
		 frameTicker = 1;
		
		
	 }	

	public void setHole(int hole,int animal){
		
		switch (Animal) {
		case RABBIT:
			 switch (hole) {
		 		case 0:	 			
		 			this.draw_x = (float) (19);
		 			this.draw_y = (float) (279);	
		 			break;
		 		case 1:
		 			this.draw_x = (float) (177);
		 			this.draw_y = (float) (279);
		 		    break;
		 		case 2:
		 			this.draw_x = (float) (343);
		 			this.draw_y = (float) (279);
		 		    break;
		 		case 3:
		 			this.draw_x = (float) (19);
		 			this.draw_y = (float) (417);
		 		    break;
		 		case 4:	 			
		 			this.draw_x = (float) (177);
		 			this.draw_y = (float) (417);
		 		    break;
		 		case 5:
		 			this.draw_x = (float) (343);
		 			this.draw_y = (float) (417);
		 		    break;
		 		case 6:
		 			this.draw_x = (float) (19);
		 			this.draw_y = (float) (553);
		 		    break;
		 		case 7:
		 			this.draw_x = (float) (177);
		 			this.draw_y = (float) (553);
		 		    break;
		 		case 8:
		 			this.draw_x = (float) (343);
		 			this.draw_y = (float) (553);
		 		    break;
		 		case 15:
		 			this.draw_x = (float) (218);
		 			this.draw_y = (float) (81);
		 		    break;
			 }			 						
			break;
		case SQRL:			
			 switch (hole) {
		 		case 0:	 			
		 			this.draw_x = (float) (19);
		 			this.draw_y = (float) (303);	
		 			break;
		 		case 1:
		 			this.draw_x = (float) (177);
		 			this.draw_y = (float) (303);
		 		    break;
		 		case 2:
		 			this.draw_x = (float) (343);
		 			this.draw_y = (float) (303);
		 		    break;
		 		case 3:
		 			this.draw_x = (float) (19);
		 			this.draw_y = (float) (441);
		 		    break;
		 		case 4:	 			
		 			this.draw_x = (float) (177);
		 			this.draw_y = (float) (441);
		 		    break;
		 		case 5:
		 			this.draw_x = (float) (343);
		 			this.draw_y = (float) (441);
		 		    break;
		 		case 6:
		 			this.draw_x = (float) (19);
		 			this.draw_y = (float) (578);
		 		    break;
		 		case 7:
		 			this.draw_x = (float) (177);
		 			this.draw_y = (float) (578);
		 		    break;
		 		case 8:
		 			this.draw_x = (float) (343);
		 			this.draw_y = (float) (578);
		 		    break;
		 		case 15:
		 			this.draw_x = (float) (218);
		 			this.draw_y = (float) (108);
		 		    break;
			 }			 		
			break;
		default:
			break;
		}
				
		this.draw_x = panel.convertX(draw_x);
		this.draw_y = panel.convertY(draw_y);
	}

	public int getHole(){
		return this.hole;
	}

	public boolean isTouched(){
		return this.touched;
	}
	
	public float getWidth(){
		return rabbitComingOutBitmaps.get(0).getWidth();
	}
	
	public float getHeight(){
		return rabbitComingOutBitmaps.get(0).getHeight();
	}
			
	public boolean isThisMoleToHit(){
		if (this.Animal == gs.getMoleToHit())
			return true;
		return false;
	}
	
	public void setTouched(boolean touched){
		this.touched=touched;
	}
	
	public void setComingOut(boolean comingOut,int level){
		this.comingOut = comingOut;
		currentFrame = 0;
		if (level < 5)
			repeatingFrames = 40;
		else 
			repeatingFrames = 40 - level/2;
		
				
	}
	
	public boolean isComingOut() {
		return this.comingOut;
	}
	
	public void setAnimal() {
		this.Animal = r.nextInt(2);		
		//this.Animal = RABBIT;
		switch (Animal) {
		case RABBIT:			
			 this.rabbitComingOutBitmaps = WhackEmPanel.rabbitComingAnimation;		 		 
			 this.rabbitDazedBitmaps = WhackEmPanel.rabbitDazedAnimation;
			 rabbitComingOutframeNr = rabbitComingOutBitmaps.size();		 
			 rabbitDazedFrameNr = rabbitDazedBitmaps.size();			 
			break;
		case SQRL:
			 this.rabbitComingOutBitmaps = WhackEmPanel.sqrlComingAnimation;		 		 
			 this.rabbitDazedBitmaps = WhackEmPanel.sqrlDazedAnimation;
			 rabbitComingOutframeNr = sqrlComingOutBitmaps.size();		 
			 rabbitDazedFrameNr = sqrlDazedBitmaps.size();
			 
			break;
		default:
			break;
		}				
		 rabbitComingOutframeNr = rabbitComingOutBitmaps.size();		 
		 rabbitDazedFrameNr = rabbitDazedBitmaps.size();
		 width = getWidth();
		 height = getHeight();
		
	}
	

	public void setAnimal(int Animal) {
		
		this.Animal = Animal;		
		//this.Animal = RABBIT;
		switch (Animal) {
		case RABBIT:			
			 this.rabbitComingOutBitmaps = WhackEmPanel.rabbitComingAnimation;		 		 
			 this.rabbitDazedBitmaps = WhackEmPanel.rabbitDazedAnimation;
			 rabbitComingOutframeNr = rabbitComingOutBitmaps.size();		 
			 rabbitDazedFrameNr = rabbitDazedBitmaps.size();			 
			break;
		case SQRL:
			 this.rabbitComingOutBitmaps = WhackEmPanel.sqrlComingAnimation;		 		 
			 this.rabbitDazedBitmaps = WhackEmPanel.sqrlDazedAnimation;
			 rabbitComingOutframeNr = sqrlComingOutBitmaps.size();		 
			 rabbitDazedFrameNr = sqrlDazedBitmaps.size();
			 
			break;
		default:
			break;
		}		
		 rabbitComingOutframeNr = rabbitComingOutBitmaps.size();		 
		 rabbitDazedFrameNr = rabbitDazedBitmaps.size();
		 //setComingOut(true, GameState.getLevel());
		
	}
		
	
	public void setDazed(boolean dazed){
		
		if (currentFrame !=0) { 
			if (isThisMoleToHit()) { 
				//GameState.addPoints((60/currentFrame) * GameState.getLevel());
				gs.addMoleHitCounter(1);
				gs.rightMolesCounter++;
				} 
			else {
				//GameState.addPoints(-(60/currentFrame) * GameState.getLevel());				
				gs.wrongMolesCounter++;
				panel.removeLife();
			}
		}
		setComingOut(false, 0);
		this.dazed = dazed;
		currentFrame = 0;
	}
	
	public boolean isDazed() {
		return this.comingOut;
	}
	
	public boolean isAnimated(){ 
		return (this.comingOut || this.dazed);
	}
		
	public void update(long gameTime) {		
		setHole(hole,this.Animal);
		if (comingOut){
			if (gameTime > frameTicker + framePeriod) {
				frameTicker = gameTime;
				// increment the frame
				currentFrame++;					
				if (currentFrame == 17) {
				
					if (repeatingFrames>0){
						currentFrame--;
						repeatingFrames--;
					}
				}
					
				if (currentFrame >= rabbitComingOutframeNr) {
					if (isThisMoleToHit())
						panel.removeLife();
					currentFrame = 0;
					setComingOut(false,0);
					setAnimal();
					
				}			
								
				
			}
			toDrawBitmap = rabbitComingOutBitmaps.get(currentFrame);
		} else if (dazed){
			if (gameTime > frameTicker + framePeriod) {
				frameTicker = gameTime;
				// increment the frame
				currentFrame++;					
				}
					
				if (currentFrame >= rabbitDazedFrameNr) {
					currentFrame = 0;
					setDazed(false);
					setAnimal();
				}			
				toDrawBitmap = rabbitDazedBitmaps.get(currentFrame);
		}		
		else {		
			currentFrame = 0;
			// define which bitmap should be drawn
			toDrawBitmap = rabbitComingOutBitmaps.get(currentFrame);
		}		
	}

	
	public void drawMole(Canvas canvas){
		  if (isAnimated())
			canvas.drawBitmap(toDrawBitmap, draw_x, draw_y, null);
			
	}
	
	public void handleActionDown(float eventX, float eventY) {
		Log.d(TAG, "mole touched:"  + eventX + "," + eventY );		
		if (eventX >= (draw_x ) && (eventX <= (draw_x + width))) {
		   if (eventY >= (draw_y) +40 && (eventY <= (draw_y + height -20 ))) {
			if (isComingOut() && currentFrame !=0){
				Log.d(TAG, "mole touched" + this.hole);
				setDazed(true);
				
			}
		   } else 
		    setTouched(false);		   
	    } else 
		    setTouched(false);
	}
	
	
	
 }